﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Linq;
using DataAccessLayer;
namespace BusinessAccessLayer
{
    public class BAHoaDon
    {
        QuanLyBanHangDataContext dbs=new QuanLyBanHangDataContext();
        
        public DataTable getTable()
        {
            DataTable hd=new DataTable();
            hd.Columns.Add("MaHD",typeof(string));
            hd.Columns.Add("MaKH",typeof(string));
            hd.Columns.Add("MaNV",typeof(string));
            hd.Columns.Add("NgayLapHD",typeof(DateTime));
            hd.Columns.Add("NgayNhanHang",typeof(DateTime));

            foreach (var item in dbs.HoaDons.ToList())
            {
                DataRow row = hd.NewRow();
                row["MaHD"] = item.MaHD;
                row["MaKH"] = item.MaKH;
                row["MaNV"] = item.MaNV;
                row["NgayLapHD"] = item.NgayLapHD;
                row["NgayNhanHang"] = item.NgayNhanHang;
                hd.Rows.Add(row);
            }
            return hd;
        }
        public bool ThemHoaDon(string strMaHD, string strMaKH, string strMaNV, DateTime strNgayLapHD, DateTime strNgayNhapHang)
        {
            bool f = false;
            try
            {
                HoaDon hd = new HoaDon()
                {
                    MaHD=strMaHD,
                    MaKH=strMaKH,
                    MaNV=strMaNV,
                    NgayLapHD=strNgayLapHD,
                    NgayNhanHang=strNgayNhapHang
                };
                dbs.HoaDons.InsertOnSubmit(hd);
                dbs.SubmitChanges();
                f = true;
            }
            catch
            {
                f = false;
            }
            return f;
        }
        public bool XoaHoaDon(string strMaHD)
        {
            bool f = false;
            try
            {
                HoaDon hoadon = dbs.HoaDons.Single(hd => hd.MaHD == strMaHD);
                dbs.HoaDons.DeleteOnSubmit(hoadon);
                dbs.SubmitChanges();
                f = true;
            }
            catch
            {
                f = false;
            }
            return f;
        }
        public bool CapNhatHoaDon(string strMaHD, string strMaKH, string strMaNV, DateTime strNgayLapHD, DateTime strNgayNhapHang)
        {
            bool f = false;
            try
            {
                HoaDon hoadon =
                    dbs.HoaDons.
                    Single(hd => hd.MaHD == strMaHD);
                hoadon.MaKH = strMaKH;
                hoadon.MaNV = strMaNV;
                hoadon.NgayLapHD = strNgayLapHD;
                hoadon.NgayNhanHang = strNgayNhapHang;
                dbs.SubmitChanges();
                f = true;
            }
            catch
            {
                f = false;
            }
            return f;

        }
    }
}

